package com.wxy.lianxi.springbootday01.exception;

import com.wxy.lianxi.springbootday01.pojo.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//@RestControllerAdvice是全局异常处理器专用注解,有异常就会跑到该类里
@RestControllerAdvice
public class GlobalExceptionHander {

    //这个对象要从左往右写才能出来
    Logger log = LoggerFactory.getLogger(GlobalExceptionHander.class);

    //抓取对应异常,进行处理,{Exception.class}表示只要发生异常就会来到这个ExceptionHandler
    @ExceptionHandler({Exception.class})
    public R exception(Exception e, HttpServletRequest request, HttpServletResponse response){

       log.error("统一异常处理",e);
        return new R(500, e.getMessage(), null);
    }

}
